﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

namespace NSpotifyLib
{
    internal class AlbumReader : BaseReader<Album>
    {
        internal AlbumReader(XDocument document) : base(document)
        {
        }

        public override IEnumerable<Album> Read()
        {
            var albums = from album in document.Descendants(ns+ SpotifyTags.Album)
                            select new Album
                            {
                                SpotifyLink = album.Attribute(SpotifyTags.SpotifyLink).Value,
                                Name = album.Element(ns+SpotifyTags.Name).Value,
                                Popularity = Double.Parse(album.Element(ns+SpotifyTags.Popularity).Value),
                                Artist = (from artist in album.Descendants(ns+ SpotifyTags.Artist)
                                         select new Artist
                                         {
                                           Name = artist.Element(ns+SpotifyTags.Name).Value,
                                           SpotifyLink = artist.Attribute(SpotifyTags.SpotifyLink) != null ? artist.Attribute("href").Value : String.Empty
                                         }).FirstOrDefault()
                            };

            return albums;
        }
    }
}
